﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ZetaPrints.ZetaClient.Shared.Manipulator
{
    /// <summary>
    /// Manipulation of strings to the way we want them.
    /// </summary>
    public static class StringManipulator
    {
        #region private methods
        private static string GetLastEntryEntry(string path, char separator)
        {
            string entry = string.Empty;

            if (path.Contains(separator))
            {
                entry = path.Substring(path.LastIndexOf(separator) + 1);
            }
            else
            {
                entry = path;
            }

            return entry;
        }
        #endregion


        #region public methods
        /// <summary>
        /// Gets the file name from a web address, will return only the last 
        /// entry on the web path give. (ex. http://www.google.com/myFiles/file.txt will return file.txt)
        /// </summary>
        /// <param name="webPath"></param>
        /// <returns></returns>
        public static string GetFileNameFromWebPath(string webPath)
        {
            string fileName = string.Empty;

            return GetLastEntryEntry(webPath, '/');
        }
        /// <summary>
        /// Gets the file name and path from a web file:// parameter
        /// </summary>
        /// <param name="webPath"></param>
        /// <returns></returns>
        public static string GetFilePathFromWebPath(string webPath)
        {
            string filePath = string.Empty;

            /// remove anything that comes before the // like file://
            if (webPath.Contains("///"))
            {
                filePath = webPath.Substring(webPath.IndexOf("///") + 3);
            }
            else
            {
                filePath = webPath;
            }

            /// replace all the / to \ so we have windows directory
            /// parameter
            filePath = filePath.Replace("/", @"\");

            return filePath;
        }
        #endregion
    }
}
